package cn.iocoder.devlop.module.annual.report.shouldknow.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.devlop.framework.common.pojo.PageResult;
import cn.iocoder.devlop.module.annual.report.shouldknow.controller.vo.ShouldKnowCreateReqVO;
import cn.iocoder.devlop.module.annual.report.shouldknow.controller.vo.ShouldKnowExportReqVO;
import cn.iocoder.devlop.module.annual.report.shouldknow.controller.vo.ShouldKnowPageReqVO;
import cn.iocoder.devlop.module.annual.report.shouldknow.controller.vo.ShouldKnowUpdateReqVO;
import cn.iocoder.devlop.module.annual.report.shouldknow.convert.ShouldKnowConvert;
import cn.iocoder.devlop.module.annual.report.shouldknow.dal.ShouldKnowMapper;
import cn.iocoder.devlop.module.annual.report.shouldknow.dal.dataobject.ShouldKnowDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.devlop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.devlop.module.annual.report.enums.ErrorCodeConstants.SHOULD_KNOW_NOT_EXISTS;

/**
 * 填报须知 Service 实现类
 *
 * @author sunhao
 */
@Service
@Validated
public class ShouldKnowServiceImpl implements ShouldKnowService {

    @Resource
    private ShouldKnowMapper shouldKnowMapper;

    @Override
    public Long createShouldKnow(ShouldKnowCreateReqVO createReqVO) {
        // 插入
        ShouldKnowDO shouldKnow = ShouldKnowConvert.INSTANCE.convert(createReqVO);
        shouldKnowMapper.insert(shouldKnow);
        // 返回
        return shouldKnow.getId();
    }

    @Override
    public void updateShouldKnow(ShouldKnowUpdateReqVO updateReqVO) {
        // 校验存在
        validateShouldKnowExists(updateReqVO.getId());
        // 更新
        ShouldKnowDO updateObj = ShouldKnowConvert.INSTANCE.convert(updateReqVO);
        shouldKnowMapper.updateById(updateObj);
    }

    @Override
    public void deleteShouldKnow(Long id) {
        // 校验存在
        validateShouldKnowExists(id);
        // 删除
        shouldKnowMapper.deleteById(id);
    }

    private void validateShouldKnowExists(Long id) {
        if (shouldKnowMapper.selectById(id) == null) {
            throw exception(SHOULD_KNOW_NOT_EXISTS);
        }
    }

    @Override
    public ShouldKnowDO getShouldKnow(String enttype) {
        return shouldKnowMapper.selectOne(ShouldKnowDO::getEnttype,enttype);
    }

    @Override
    public ShouldKnowDO getShouldKnow(Long id) {
        return shouldKnowMapper.selectById(id);
    }

    @Override
    public List<ShouldKnowDO> getShouldKnowList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return shouldKnowMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShouldKnowDO> getShouldKnowPage(ShouldKnowPageReqVO pageReqVO) {
        return shouldKnowMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShouldKnowDO> getShouldKnowList(ShouldKnowExportReqVO exportReqVO) {
        return shouldKnowMapper.selectList(exportReqVO);
    }

}
